package com.k8.rpc.directory;

import com.k8.rpc.Node;
import com.k8.rpc.invoke.Invoker;

import java.util.List;

public class StaticDirectory<T> implements Directory<T> {
    private final Class<T> type;
    private boolean isDestroy;
    private List<Invoker<T>> services;

    public StaticDirectory(List<Invoker<T>> invokers, Class<T> type) {
        assert invokers != null;
        assert type != null;
        this.services = invokers;
        this.type = type;
    }

    @Override
    public List<Invoker<T>> list() {
        return this.services;
    }

    @Override
    public int size() {
        return services.size();
    }

    //静态目录，不支持serviceAdd
    @Override
    public void addInvokerAddListener(InvokerAddListener invokerAddListener) {

    }

    @Override
    public void destroy() {
        if (isDestroy) return;
        synchronized (this) {
            if (isDestroy) return;
            isDestroy = true;
            services.forEach(Node::destroy);
        }
    }

    @Override
    public Class<T> type() {
        return type;
    }
}
